<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use app\modules\Order\models\OrderPriceType;
use app\modules\Order\models\OrderCustomer;
use app\modules\Order\models\Order;

$data = [];
// 获取附加费类型
$order = Order::findOne($model->order_id);
$all = OrderPriceType::find()->where(['product_id'=>$order->product_id,'type'=>1])->all();
if($all){
	//$data = yii\helpers\ArrayHelper::map($all, 'id', 'name');
	foreach($all as $da){
		$data[$da->id] = $da->name . ' / ' . Yii::$app->formatter->asCurrency($da->price);
	}
}
// 获取目前已添加的客户
$customers = [];
$all2 = OrderCustomer::find()->where(['order_id'=>$model->order_id, 'status'=>1])->all();
if($all2){
	foreach($all2 as $c){
		//$customers[$c->id] = [$c->first_name . $c->last_name, $c->roomCustomer->order_room_id  ];
		if($c->roomCustomer){ // 该用户是否分配在房间内, 未分配在房间内的视为取消用户
		
			$customers[$c->roomCustomer->order_room_id][$c->id] = $c->first_name . $c->last_name;
		}
	}
}
// 转换格式
if(!empty($model->customers)){
	if(! is_array($model->customers)){
		$model->customers = json_decode($model->customers, true);
	
	}
}else{
	$model->customers = [];
}
?>
<style type="text/css">
	.radio-inline + .radio-inline, .checkbox-inline + .checkbox-inline {margin-left: 0px;margin-right: 15px;}
	.form-horizontal .radio, .form-horizontal .checkbox, .form-horizontal .radio-inline, .form-horizontal .checkbox-inline {margin-right:15px;}
</style>
<div class="modal-header">
	<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
	<h4 class="modal-title"><small>添加附加费</small></h4>
</div>
<div class="row" style="padding:15px 15px 0px 15px;">
    <?php $form = ActiveForm::begin([
                            'options' => ['enctype' => 'multipart/form-data',],
                            'layout' => 'horizontal',
                            'fieldConfig' => [
                               'template' => "{label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}",
                               'horizontalCssClasses' => [
                                   'label' => 'col-sm-2',
                                   'wrapper' => 'col-sm-9',
                               ],
                            ],
                       ]); ?>
 
    <!-- ?= $form->field($model, 'order_id')->label(false)->hiddenInput() ? -->
	<input type="hidden" id="orderprice-order_id" class="form-control" name="OrderPrice[order_id]" value="<?= $model->order_id ?>">

	<?= $form->field($model, 'type')->radioList($data) ?>

	<div class="form-group field-orderprice-customers">
		<div class="control-label col-sm-2" for="orderprice-customers">选择客户<br><br>
			<label><input id="checkAll" type="checkbox" /> 全选</label>
		</div>
		<div class="col-sm-9">
			<input type="hidden" name="OrderPrice[customers]" >
			<div id="orderprice-customers">
				<?php 
					if($customers ){
					$arr = empty($model->customers) ? [] : $model->customers;
					foreach($customers  as $k=>$customer){ ?>
					<div class="panel" style="padding:0px 15px 5px 15px;">
					<?php 
						foreach($customer  as $key=>$val){ ?>
						<label class="checkbox-inline">
							<input type="checkbox" name="OrderPrice[customers][]" value="<?= $key?>" <?= in_array($key, $arr) ? 'checked' : '' ?> > <?= $val ?>
						</label>
					<?php }  ?>
					</div>
				<?php }} ?>
			</div>
			<div class="help-block help-block-error"></div>
		</div>
	</div>

    <div class="form-group text-center">
		<?php $btn = $model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'); 
			  $btn = '&nbsp;&nbsp;' . $btn . '&nbsp;&nbsp;';
	 	?>
        <?= Html::submitButton($btn, ['class' => $model->isNewRecord ? 'btn btn-success btn-lg' : 'btn btn-primary']) ?>
    </div>

    <?php ActiveForm::end(); ?>
</div>


<?php app\components\JsBlock::begin() ?>
<script type="text/javascript">
	$("#checkAll").click(function() {
		if($(this).is(':checked')){
		
			$('input[name="OrderPrice[customers][]"]').prop("checked", true); //this.checked 
		}else{
		
			$('input[name="OrderPrice[customers][]"]').prop("checked", false); //this.checked 
		}
	});

	var $subBox = $("input[name='OrderPrice[customers][]']");

	$subBox.click(function(){
			if($subBox.length == $("input[name='OrderPrice[customers][]']:checked").length){
				$("#checkAll").prop("checked", true);
			}else{
				$("#checkAll").prop("checked", false);
			}
	});
</script>
<?php app\components\JsBlock::end() ?>
